package com.teamtgv.shareshark;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.util.Log;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.model.GraphUser;


public class UserData {

	private static MainFragment callbackFragment;
	private static String username = null;
	private static BigInteger userid = null;
	private static HashMap<String, BigInteger> friends = new HashMap<String, BigInteger>();
	private static boolean userLoaded = false;
	private static boolean friendsLoaded = false;
	public static final String UNKNOWN_USER = "unknown";
	
	public static void loadData(Session session, MainFragment newCallbackFragment) {
		if (session == null)
			return;
		callbackFragment = newCallbackFragment;
		loadUser(session);
		loadFriends(session);
	}
	
	private static void loadUser(Session session) {
		Log.v("UserData", "Session state: " + session.getState());
		Log.v("UserData", "Loading user data");
		Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
			
			@Override
			public void onCompleted(GraphUser user, Response response) {
				if (user == null) {
					Log.v("UserData", "Retrieved \"user\" object from FB is null.");
					Log.v("UserData", "Response: " + response + " --- Error = " + response.getError().getErrorMessage());
				} else {
					username = user.getName();
					Log.v("UserData", "Loaded username: " + username);
					userid = new BigInteger(user.getId());
					Log.v("UserData", "Loaded userid: " + userid);
				}
				userLoaded = true;
				UserData.taskFinished();
			}
		
		});
		Request.executeBatchAsync(request);
		Log.v("UserData", "Loading user data task started");
		Log.v("UserData", "Loading user data task finished");
	}
	
	private static void taskFinished() {
		if (userLoaded && friendsLoaded)
			callbackFragment.callbackDataLoaded();
	}
	
	private static void loadFriends(Session session) {
		Log.v("UserData", "Loading friends");
		Request request = Request.newMyFriendsRequest(session, new Request.GraphUserListCallback() {
			
			@Override
			public void onCompleted(List<GraphUser> users, Response response) {
				for (GraphUser user : users) {
					Log.v("UserData", "Loaded friend " + user.getName());
					friends.put(user.getName(), new BigInteger(user.getId()));
				}
				Log.v("UserData", "Loaded friends");
				friendsLoaded = true;
				UserData.taskFinished();
			}
		});
		Request.executeBatchAsync(request);
		Log.v("UserData", "Loading friends data started");
	}

	public static String getUsername() {
		return new String(username);
	}
	
	public static BigInteger getUserId() {
		return userid;
	}
	
	public static List<String> getFriendList() {
		return new ArrayList<String>(friends.keySet());
	}
	
	public static BigInteger getFriendId(String name) {
		if (! friends.containsKey(name))
			return null;
		return friends.get(name);
	}
	
	public static String getName(BigInteger searchedUserId) {
		if (searchedUserId.equals(UserData.userid))
			return UserData.username;
		if (! friends.containsValue(searchedUserId))
			return UNKNOWN_USER;
		for (String friendName : friends.keySet()) {
			if (friends.get(friendName).equals(searchedUserId))
				return friendName;
		}
		return UNKNOWN_USER;
	}
	
	public static void clear() {
		username = null;
		userid = null;
		friends.clear();
	}
}
